/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddBooth.java
 *
 * Created on Mar 17, 2011, 6:23:19 PM
 */

package GUI;

import Entities.BoothInformation;
import Business.BoothManage;
import Business.SpaceUnitMeasure;
import java.sql.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Administrator
 */
public class AddBooth extends javax.swing.JDialog {

    /** Creates new form AddBooth */
    public AddBooth(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        try {
            initComponents();
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AddBooth.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(AddBooth.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(AddBooth.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(AddBooth.class.getName()).log(Level.SEVERE, null, ex);
        }
          objbooth=new BoothManage();
         // Exhibitor=new ExhibitorManage();
          space=new SpaceUnitMeasure();
          FillDatatoComBo();
          FilldataToComboExhiBitor();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void FilldataToComboExhiBitor()
    {
      for(String item : objbooth.getExhibitorToArrayList())
          cbxExhibitor.addItem(item);
    }
    private void FillDatatoComBo()
    {
      for(String item:space.getListToVector())
          cbxSpaceMeasure.addItem(item);
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtBoothName = new javax.swing.JTextField();
        txtSponsor = new javax.swing.JTextField();
        txtSpace = new javax.swing.JTextField();
        txtFees = new javax.swing.JTextField();
        bntAdd = new javax.swing.JButton();
        bntReset = new javax.swing.JButton();
        bntCancel = new javax.swing.JButton();
        txtContractCode = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        cbxSpaceMeasure = new javax.swing.JComboBox();
        cbxExhibitor = new javax.swing.JComboBox();
        cbStatus = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel2.setText("Booth Name");

        jLabel5.setText("Space");

        jLabel6.setText("SpaceMeasure ");

        jLabel7.setText("Fees");

        jLabel8.setText("Contract Code");

        jLabel3.setText("Sponsors");

        jLabel4.setText("Exhibitor ");

        bntAdd.setText("Add");
        bntAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntAddActionPerformed(evt);
            }
        });

        bntReset.setText("Reset");
        bntReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntResetActionPerformed(evt);
            }
        });

        bntCancel.setText("Cancel");
        bntCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelActionPerformed(evt);
            }
        });

        jLabel9.setText("Status");

        cbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Normal", "Block" }));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cbxSpaceMeasure, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(txtFees)
                                .addGap(1, 1, 1))
                            .addComponent(txtSpace)
                            .addComponent(txtBoothName, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(112, 112, 112)
                        .addComponent(bntAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(txtSponsor, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(57, 57, 57)
                        .addComponent(cbStatus, 0, 118, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                            .addComponent(bntReset))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cbxExhibitor, 0, 118, Short.MAX_VALUE)
                                    .addComponent(txtContractCode, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addComponent(bntCancel)))))
                .addGap(62, 62, 62))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtBoothName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtSpace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(cbxSpaceMeasure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(cbxExhibitor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(txtContractCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtFees, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtSponsor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bntAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bntReset)
                    .addComponent(bntCancel))
                .addGap(70, 70, 70))
        );

        jLabel13.setText("                 ADD a Booth");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(21, 21, 21))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(95, 95, 95))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bntResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntResetActionPerformed
        // TODO add your handling code here:
        SetText();
    }//GEN-LAST:event_bntResetActionPerformed

    private void bntCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_bntCancelActionPerformed

    private void bntAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntAddActionPerformed
        // TODO add your handling code here:
     if(FormProcessing()){
        Date todayD=new Date(System.currentTimeMillis());
        BoothInformation B=new BoothInformation();
        B.setName(txtBoothName.getText());
        B.setSpace(Integer.parseInt(txtSpace.getText()));
     
        B.setSpaceUnitMeasureID(space.GetID(cbxSpaceMeasure.getSelectedItem().toString()));
        B.setFees(Double.parseDouble(txtFees.getText()));
        String[] strID = cbxExhibitor.getSelectedItem().toString().split("-");
        B.setExhibitorID(Integer.parseInt(strID[0]));
        B.setSponsor(txtSponsor.getText());
        B.setContractCode(Integer.parseInt(txtContractCode.getText()));
        if(cbStatus.getSelectedIndex()>-1){
           if(cbStatus.getSelectedItem().equals("Normal"))
             B.setStatus(1);
           else
               if(cbStatus.getSelectedItem().equals("Block"))
                    B.setStatus(0);
        }
        B.setRegisterDate(todayD);
        B.setMoDifiedDate(todayD);
        if(objbooth.AddBooth(B))
        {
           JOptionPane.showMessageDialog(rootPane, "Success");
        }
      
       else
           JOptionPane.showMessageDialog(rootPane, "please check data fields !");
        }
    }//GEN-LAST:event_bntAddActionPerformed
   private void SetText()
    {
      
       txtBoothName.setText("");
       txtSpace.setText("");
       txtFees.setText("");
       txtSponsor.setText("");
       txtContractCode.setText("");
    }
    private boolean isValidNumber(String number){
       String  expression="[0-9]{1,20}";
       CharSequence inputStr = number;
       Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
       Matcher matcher = pattern.matcher(inputStr);
       return matcher.matches();
   }
 private boolean FormProcessing()
 {
        if(txtBoothName.getText().length()==0)
         {
            JOptionPane.showMessageDialog(rootPane, "Booth Name field is not blank !");
            txtBoothName.requestFocusInWindow();
            return false;
         }
       else if (txtSpace.getText().length() == 0)
         {
            JOptionPane.showMessageDialog(rootPane, "Booth space field is not blank !");
            txtSpace.requestFocusInWindow();
            return false;
         }
       else if(!isValidNumber(txtSpace.getText()))
        {
            JOptionPane.showMessageDialog(rootPane, "Booth space field is must numbers !");
            txtSpace.requestFocusInWindow();
            return false;
        }
        else if(txtFees.getText().length()==0)
          {
             JOptionPane.showMessageDialog(rootPane, "Booth Fees field is not blank !");
             txtFees.requestFocusInWindow();
             return false;
         }
       else if (!isValidNumber(txtFees.getText()))
          {
            JOptionPane.showMessageDialog(rootPane, "Booth fees field is must numbers  !");
            txtFees.requestFocusInWindow();
         }
        else if (txtSponsor.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(rootPane, "Booth sponsor field is not blank !");
            txtSponsor.requestFocusInWindow();
            return false;
        }
     else if(txtContractCode.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(rootPane, "ContractCode field is not blank !");
            txtContractCode.requestFocusInWindow();
            return false;
        }
     else if (!isValidNumber(txtContractCode.getText()))
        {
           JOptionPane.showMessageDialog(rootPane, "ContractCode field is must numbers  !");
            txtContractCode.requestFocusInWindow();
            return false;
        }
   return true;
 }
    /**
    * @param args the command line arguments
    */
  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntAdd;
    private javax.swing.JButton bntCancel;
    private javax.swing.JButton bntReset;
    private javax.swing.JComboBox cbStatus;
    private javax.swing.JComboBox cbxExhibitor;
    private javax.swing.JComboBox cbxSpaceMeasure;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtBoothName;
    private javax.swing.JTextField txtContractCode;
    private javax.swing.JTextField txtFees;
    private javax.swing.JTextField txtSpace;
    private javax.swing.JTextField txtSponsor;
    // End of variables declaration//GEN-END:variables
    BoothManage objbooth;
    SpaceUnitMeasure space;
    //ExhibitorManage Exhibitor;
}
